home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / ICNDRW_1.ARJ / ICONTOOL.PAS < prev    next >
Pascal/Delphi Source File  |  1991-08-20  |  17KB  |  729 lines

  1. {=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  2.                                  ------------
  3.                                   ICON TOOLS
  4.                                  ------------
  5.  
  6.                   Copyright (c) 1991 - SofDesign Technology
  7.                              All Rights Reserved
  8.  
  9.         ICON TOOLS is a programmers library for manipulating icons
  10.         created with ICON DRAW 1.0.  With this library you can load
  11.         icons from either .ICN bit mapped files or from .ILB icon
  12.         library files created with the ICONLIB program.  Each icon
  13.         is considered to be an object, hence the fully onject oriented
  14.         nature of this library.  With an ICON object, you can drag,
  15.         move and manipulate the icon in a number of ways.  This
  16.         unit is good for creating a graphically oriented icon-based
  17.         GUI, though it's up to you to do the rest.
  18.  
  19.  =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
  20.  
  21. {$I-}
  22. {$X+}
  23. {$G+}
  24. {$S-}
  25. {$R-}
  26.  
  27. unit icontool;
  28.  
  29. interface
  30.  
  31. uses mouse,graph,ttools;
  32.  
  33. const
  34.   titleon : boolean = true;
  35.   titleoff: boolean = false;
  36.  
  37. type
  38.  
  39.   icon_record = record
  40.     len,
  41.     height : integer;
  42.     fname  : string[12];
  43.     title  : string[25];
  44.     size   : longint;
  45.   end;
  46.  
  47.   header_type = record
  48.     version : string[10];
  49.     numicons : word;
  50.   end;
  51.  
  52.   icon_window = record
  53.     left,
  54.     top,
  55.     bottom,
  56.     right   : integer;
  57.   end;
  58.  
  59.   {----------------------------------------------------------
  60.         The icon object.  To initialize, you specify
  61.         a library name and an icon name within the
  62.         library.  If no library is indicated, then
  63.         the object searches the directory for the
  64.         icon itself.  If in either case the icon cannot
  65.         be loaded, and error flag is set and can be
  66.         checked with the INITERROR function.
  67.    ----------------------------------------------------------}
  68.   icon = object
  69.     x,y        : integer;
  70.     theicon    : icon_record;
  71.     error      : boolean;
  72.     put_type   : word;
  73.     titleon    : boolean;
  74.     hidden     : boolean;
  75.     libheader  : header_type;
  76.     boundaries : icon_window;
  77.  
  78.     procedure init(iconlib:string; iconfile:string; startx,starty: integer);
  79.     function  initerror:boolean;
  80.     function  inbounds(mx,my:integer):boolean;
  81.     function  is_hidden:boolean;
  82.     function  is_showing:boolean;
  83.     function  get_length:integer;
  84.     function  get_height:integer;
  85.     function  getx:integer;
  86.     function  gety:integer;
  87.     function  gettop:integer;
  88.     function  getleft:integer;
  89.     function  getbottom:integer;
  90.     function  getright:integer;
  91.     procedure setboundaries(x1,y1,x2,y2:integer);
  92.     procedure showicon;
  93.     procedure hideicon;
  94.     procedure icontitle(on:boolean);
  95.     procedure setput(put:word);
  96.     procedure setxy(newx,newy:integer);
  97.     procedure moveicon(newx,newy:integer);
  98.     procedure mousedragicon(onbutton:word);
  99.     procedure display_icon;
  100.     procedure disposeicon;
  101.     procedure done;
  102.   private
  103.     image_behind : pointer;
  104.     icon_image   : pointer;
  105.     title_image  : pointer;
  106.     size,
  107.     size_title_image : longint;
  108.     procedure get_rear_image;
  109.     procedure get_title_image;
  110.     procedure dispose_rear_image;
  111.     procedure dispose_title_image;
  112.   end;
  113.  
  114.   icon_manager = object(icon)
  115.     icons : array [1..500] of ^icon;
  116.     numicons : integer;
  117.     ontop : boolean;
  118.     mboundaries : icon_window;
  119.  
  120.     procedure init(left,top,bottom,right:integer);
  121.     procedure addicon(iconlib:string; iconfile:string; startx,starty: integer; put:word; t:boolean);
  122.     procedure allowontop(on:boolean);
  123.     function  initerror(i:integer):boolean;
  124.     function  inbounds(i:integer; mx,my:integer):boolean;
  125.     function  onanother(i:integer):boolean;
  126.     function  onwhichicon(mx,my:word):integer;
  127.     function  is_hidden(i:integer):boolean;
  128.     function  is_showing(i:integer):boolean;
  129.     function  get_length(i:integer):integer;
  130.     function  get_height(i:integer):integer;
  131.     function  getx(i:integer):integer;
  132.     function  gety(i:integer):integer;
  133.     function  gettop(i:integer):integer;
  134.     function  getleft(i:integer):integer;
  135.     function  getbottom(i:integer):integer;
  136.     function  getright(i:integer):integer;
  137.     function  currenticon:integer;
  138.     function  withinboundaries(mx,my:integer):boolean;
  139.     procedure setmanagerboundaries(x1,y1,x2,y2:integer);
  140.     procedure setboundaries(i,x1,y1,x2,y2:integer);
  141.     procedure showicon(i:integer);
  142.     procedure hideicon(i:integer);
  143.     procedure icontitle(i:integer; on:boolean);
  144.     procedure setput(i:integer; put:word);
  145.     procedure setxy(i:integer; newx,newy:integer);
  146.     procedure moveicon(i:integer; newx,newy:integer);
  147.     procedure mousedragicon(onbutton:word);
  148.     procedure display_icon(i:integer);
  149.     procedure disposeicon(i:integer);
  150.     procedure showallicons;
  151.     procedure hideallicons;
  152.     procedure disposeallicons;
  153.     procedure done;
  154.   end;
  155.  
  156. var
  157.   amouse : boolean;
  158.  
  159. implementation
  160.  
  161. procedure read_lib_header(var f:file; var lh:header_type);
  162. begin
  163.   blockread(f,lh,sizeof(lh));
  164. end;
  165.  
  166. procedure get_lib_icon(var f:file; lh:header_type; iconame: string; var icon:icon_record; var err:boolean);
  167. var i:integer;
  168.     found : boolean;
  169.     t:icon_record;
  170.     p:pointer;
  171. begin
  172.   i:=0;
  173.   found:=false;
  174.   repeat
  175.     inc(i);
  176.     blockread(f,t,sizeof(t));
  177.     found:=t.fname=iconame;
  178.     if not found then
  179.     begin
  180.       getmem(p,t.size);
  181.       blockread(f,p^,t.size);
  182.       freemem(p,t.size);
  183.     end;
  184.   until (found) or (i=lh.numicons);
  185.   if found then
  186.   begin
  187.     icon:=t;
  188.     err:=false;
  189.   end
  190.   else
  191.     err:=true;
  192. end;
  193.  
  194. procedure icon.init(iconlib:string; iconfile:string; startx,starty:integer);
  195. var f:file;
  196. begin
  197.   x:=startx;
  198.   y:=starty;
  199.   iconlib:=upstring(iconlib,1);
  200.   iconfile:=upstring(iconfile,1);
  201.   if iconlib='' then
  202.   begin
  203.     assign(f,iconfile);
  204.     reset(f,1);
  205.     if ioresult<>0 then
  206.       error:=true
  207.     else
  208.     begin
  209.       hidden:=true;
  210.       error:=false;
  211.       blockread(f,theicon,sizeof(theicon));
  212.       getmem(icon_image,theicon.size);
  213.       blockread(f,icon_image^,theicon.size);
  214.       close(f);
  215.     end;
  216.   end
  217.   else
  218.   begin
  219.     assign(f,iconlib);
  220.     reset(f,1);
  221.     if ioresult<>0 then
  222.       error:=true
  223.     else
  224.     begin
  225.       read_lib_header(f,libheader);
  226.       get_lib_icon(f,libheader,iconfile,theicon,error);
  227.       if not error then
  228.       begin
  229.         hidden:=true;
  230.         getmem(icon_image,theicon.size);
  231.         blockread(f,icon_image^,theicon.size);
  232.       end;
  233.       close(f);
  234.     end;
  235.   end;
  236. end;
  237.  
  238. function icon.initerror;
  239. begin
  240.   initerror:=error;
  241. end;
  242.  
  243. function icon.is_hidden:boolean;
  244. begin
  245.   is_hidden:=hidden;
  246. end;
  247.  
  248. function icon.is_showing:boolean;
  249. begin
  250.   is_showing:=not hidden;
  251. end;
  252.  
  253. function icon.get_length:integer;
  254. begin
  255.   get_length:=theicon.len;
  256. end;
  257.  
  258. function icon.get_height:integer;
  259. begin
  260.   get_height:=theicon.height;
  261. end;
  262.  
  263. function icon.getx:integer;
  264. begin
  265.   getx:=x;
  266. end;
  267.  
  268. function icon.gety:integer;
  269. begin
  270.   gety:=y;
  271. end;
  272.  
  273. function icon.gettop:integer;
  274. begin
  275.    gettop:=gety;
  276. end;
  277.  
  278. function icon.getleft:integer;
  279. begin
  280.   getleft:=getx;
  281. end;
  282.  
  283. function icon.getbottom:integer;
  284. begin
  285.   getbottom:=gety+get_height;
  286. end;
  287.  
  288. function icon.getright:integer;
  289. begin
  290.   getright:=getx+get_length;
  291. end;
  292.  
  293. function icon.inbounds(mx,my:integer):boolean;
  294. begin
  295.   inbounds:=((mx>=x) and (mx<=x+theicon.len)) and
  296.             ((my>=y) and (my<=y+theicon.height));
  297. end;
  298.  
  299. procedure icon.get_rear_image;
  300. begin
  301.   size:=imagesize(x,y,x+theicon.len,y+theicon.height);
  302.   getmem(image_behind,size);
  303.   getimage(x,y,x+theicon.len,y+theicon.height,image_behind^);
  304. end;
  305.  
  306. procedure icon.dispose_rear_image;
  307. begin
  308.   freemem(image_behind,size);
  309. end;
  310.  
  311. procedure icon.get_title_image;
  312. begin
  313.   size_title_image:=imagesize(x-1,y+theicon.height,x+length(theicon.title)*8,y+theicon.height+10);
  314.   getmem(title_image,size_title_image);
  315.   getimage(x-1,y+theicon.height,x+length(theicon.title)*8,y+theicon.height+10,title_image^);
  316. end;
  317.  
  318. procedure icon.dispose_title_image;
  319. begin
  320.   freemem(title_image,size_title_image);
  321. end;
  322.  
  323. procedure icon.setboundaries(x1,y1,x2,y2:integer);
  324. begin
  325.   with boundaries do
  326.   begin
  327.     left:=x1;
  328.     top:=y1;
  329.     right:=x2;
  330.     bottom:=y2;
  331.     {x:=left+1;
  332.     y:=top+1;}
  333.   end;
  334. end;
  335.  
  336. procedure icon.showicon;
  337. begin
  338.   if (not initerror) and (is_hidden) then
  339.   begin
  340.     hidden:=false;
  341.     get_rear_image;
  342.     display_icon;
  343.     if titleon then
  344.     begin
  345.       get_title_image;
  346.       setfillstyle(solidfill,0);
  347.       setcolor(getmaxcolor);
  348.       bar(x-1,y+theicon.height,x+length(theicon.title)*8,y+theicon.height+10);
  349.       outtextxy(x,y+theicon.height+1,theicon.title);
  350.     end;
  351.   end;
  352. end;
  353.  
  354. procedure icon.hideicon;
  355. begin
  356.   if (not initerror) and (is_showing) then
  357.   begin
  358.     hidden:=true;
  359.     putimage(x,y,image_behind^,normalput);
  360.     dispose_rear_image;
  361.     if titleon then
  362.     begin
  363.       putimage(x-1,y+theicon.height,title_image^,copyput);
  364.       dispose_title_image;
  365.     end;
  366.   end;
  367. end;
  368.  
  369. procedure icon.icontitle(on:boolean);
  370. begin
  371.   titleon:=on;
  372.   if (theicon.title='') and (titleon=true) then
  373.     titleon:=false;
  374. end;
  375.  
  376. procedure icon.setput(put:word);
  377. begin
  378.   put_type:=put;
  379. end;
  380.  
  381. procedure icon.setxy(newx,newy:integer);
  382. begin
  383.   x:=newx;
  384.   y:=newy;
  385. end;
  386.  
  387. procedure icon.moveicon(newx,newy:integer);
  388. begin
  389.   if not initerror then
  390.   begin
  391.     hideicon;
  392.     if newx<boundaries.left then
  393.       newx:=boundaries.left;
  394.     if newy<boundaries.top then
  395.       newy:=boundaries.top;
  396.     if newx+get_length>boundaries.right then
  397.       newx:=boundaries.right-(get_length);
  398.     if newy+get_height>boundaries.bottom then
  399.       newy:=boundaries.bottom-(get_height);
  400.     setxy(newx,newy);
  401.     showicon;
  402.   end;
  403. end;
  404.  
  405. procedure icon.mousedragicon(onbutton:word);
  406. var mx,my,b:word;
  407.     oldx,oldy : word;
  408. begin
  409.   if amouse then
  410.   begin
  411.     oldx:=0;
  412.     oldy:=0;
  413.     getmouse(mx,my,b);
  414.     if b=onbutton then
  415.     begin
  416.       hidemouse;
  417.       setmouse(x,y);
  418.       while b=onbutton do
  419.       begin
  420.         getmouse(mx,my,b);
  421.         if (oldx<>mx) or (oldy<>my) then
  422.         begin
  423.           oldx:=mx;
  424.           oldy:=my;
  425.           moveicon(mx,my);
  426.         end;
  427.       end;
  428.       showmouse;
  429.     end;
  430.   end;
  431. end;
  432.  
  433. procedure icon.display_icon;
  434. begin
  435.   putimage(x,y,icon_image^,put_type);
  436. end;
  437.  
  438. procedure icon.disposeicon;
  439. begin
  440.   if not initerror then
  441.   begin
  442.     freemem(icon_image,theicon.size);
  443.     if not hidden then
  444.       dispose_rear_image;
  445.   end;
  446. end;
  447.  
  448. procedure icon.done;
  449. begin
  450. end;
  451.  
  452. procedure icon_manager.init(left,top,bottom,right:integer);
  453. begin
  454.   numicons :=0;
  455.   setmanagerboundaries(left,top,bottom,right);
  456. end;
  457.  
  458. procedure icon_manager.addicon(iconlib:string; iconfile:string; startx,starty:integer; put:word; t:boolean);
  459. begin
  460.   inc(numicons);
  461.   new(icons[numicons]);
  462.   icons[numicons]^.init(iconlib,iconfile,startx,starty);
  463.   if initerror(numicons) then
  464.   begin
  465.     dispose(icons[numicons]);
  466.     dec(numicons);
  467.   end
  468.   else
  469.   begin
  470.     with icons[numicons]^ do
  471.     begin
  472.       setput(put);
  473.       icontitle(t);
  474.       with mboundaries do
  475.         setboundaries(left,top,right,bottom);
  476.     end;
  477.     setxy(numicons,startx,starty);
  478.   end;
  479. end;
  480.  
  481. function icon_manager.onanother(i:integer):boolean;
  482. var j:integer;
  483.     found : boolean;
  484. begin
  485.   if numicons>0 then
  486.   begin
  487.     found:=false;
  488.     j:=0;
  489.     repeat
  490.       inc(j);
  491.       if j<>i then
  492.         found:=((icons[i]^.getleft>=icons[j]^.getleft) and (icons[i]^.getright<=icons[j]^.getright)) or
  493.                ((icons[i]^.gettop>=icons[j]^.gettop) and (icons[i]^.getbottom<=icons[j]^.getbottom));
  494.     until found or (j=numicons);
  495.     onanother:=found;
  496.   end;
  497. end;
  498.  
  499. procedure icon_manager.allowontop(on:boolean);
  500. begin
  501.   ontop:=on;
  502. end;
  503.  
  504. function icon_manager.initerror(i:integer):boolean;
  505. begin
  506.   if numicons>0 then
  507.     initerror:=icons[i]^.initerror;
  508. end;
  509.  
  510. function icon_manager.is_hidden(i:integer):boolean;
  511. begin
  512.   if numicons>0 then
  513.     is_hidden:=icons[i]^.is_hidden;
  514. end;
  515.  
  516. function icon_manager.is_showing(i:integer):boolean;
  517. begin
  518.   if numicons>0 then
  519.     is_showing:=icons[i]^.is_showing;
  520. end;
  521.  
  522. function icon_manager.get_length(i:integer):integer;
  523. begin
  524.   if numicons>0 then
  525.     get_length:=icons[i]^.get_length;
  526. end;
  527.  
  528. function icon_manager.get_height(i:integer):integer;
  529. begin
  530.   if numicons>0 then
  531.     get_height:=icons[i]^.get_height;
  532. end;
  533.  
  534. function icon_manager.getx(i:integer):integer;
  535. begin
  536.   if numicons>0 then
  537.     getx:=icons[i]^.getx;
  538. end;
  539.  
  540. function icon_manager.gety(i:integer):integer;
  541. begin
  542.   if numicons>0 then
  543.     gety:=icons[i]^.gety;
  544. end;
  545.  
  546. function icon_manager.gettop(i:integer):integer;
  547. begin
  548.   if numicons>0 then
  549.     gettop:=icons[i]^.gettop;
  550. end;
  551.  
  552. function icon_manager.getleft(i:integer):integer;
  553. begin
  554.   if numicons>0 then
  555.     getleft:=icons[i]^.getleft;
  556. end;
  557.  
  558. function icon_manager.getbottom(i:integer):integer;
  559. begin
  560.   if numicons>0 then
  561.     getbottom:=icons[i]^.getbottom;
  562. end;
  563.  
  564. function icon_manager.getright(i:integer):integer;
  565. begin
  566.   if numicons>0 then
  567.     getright:=icons[i]^.getright;
  568. end;
  569.  
  570. function icon_manager.inbounds(i:integer; mx,my:integer):boolean;
  571. begin
  572.   if numicons>0 then
  573.     inbounds:=icons[i]^.inbounds(mx,my);
  574. end;
  575.  
  576. function icon_manager.currenticon:integer;
  577. begin
  578.   currenticon:=numicons;
  579. end;
  580.  
  581. function  icon_manager.withinboundaries(mx,my:integer):boolean;
  582. begin
  583.   with mboundaries do
  584.     withinboundaries:=((mx>=left) and (mx<=right)) and
  585.                       ((my>=top)  and (my<=bottom));
  586. end;
  587.  
  588. procedure icon_manager.setboundaries(i,x1,y1,x2,y2:integer);
  589. begin
  590.   if numicons>0 then
  591.     icons[i]^.setboundaries(x1,y1,x2,y2);
  592. end;
  593.  
  594. procedure icon_manager.setmanagerboundaries(x1,y1,x2,y2:integer);
  595. begin
  596.   with mboundaries do
  597.   begin
  598.     top:=y1;
  599.     left:=x1;
  600.     bottom:=y2;
  601.     right:=x2;
  602.   end;
  603. end;
  604.  
  605. procedure icon_manager.showicon(i:integer);
  606. begin
  607.   if numicons>0 then
  608.     icons[i]^.showicon;
  609. end;
  610.  
  611. procedure icon_manager.hideicon(i:integer);
  612. begin
  613.   if numicons>0 then
  614.     icons[i]^.hideicon;
  615. end;
  616.  
  617. procedure icon_manager.icontitle(i:integer; on:boolean);
  618. begin
  619.   if numicons>0 then
  620.     icons[i]^.icontitle(on);
  621. end;
  622.  
  623. procedure icon_manager.setput(i:integer; put:word);
  624. begin
  625.   if numicons>0 then
  626.     icons[i]^.setput(put);
  627. end;
  628.  
  629. procedure icon_manager.setxy(i:integer; newx,newy:integer);
  630. begin
  631.   if numicons>0 then
  632.     icons[i]^.setxy(newx,newy);
  633. end;
  634.  
  635. procedure icon_manager.moveicon(i:integer; newx,newy:integer);
  636. begin
  637.   if numicons>0 then
  638.     icons[i]^.moveicon(newx,newy);
  639. end;
  640.  
  641. function icon_manager.onwhichicon(mx,my:word):integer;
  642. var found:boolean;
  643.     i:integer;
  644. begin
  645.   i:=0;
  646.   if numicons>0 then
  647.   begin
  648.     found:=false;
  649.     repeat
  650.       inc(i);
  651.       found:=inbounds(i,mx,my);
  652.     until found or (i=numicons);
  653.     if not found then
  654.       i:=0;
  655.   end;
  656.   onwhichicon:=i;
  657. end;
  658.  
  659. procedure icon_manager.mousedragicon(onbutton:word);
  660. var oldx,oldy,i : integer;
  661.     mx,my,b:word;
  662. begin
  663.   if amouse then
  664.     if numicons>0 then
  665.     begin
  666.       getmouse(mx,my,b);
  667.       i:=onwhichicon(mx,my);
  668.       if i>0 then
  669.       begin
  670.         oldx:=icons[i]^.getx;
  671.         oldy:=icons[i]^.gety;
  672.         icons[i]^.mousedragicon(onbutton);
  673.         if onanother(i) and (not ontop) then
  674.         begin
  675.           hideicon(i);
  676.           setxy(i,oldx,oldy);
  677.           showicon(i);
  678.         end;
  679.       end;
  680.     end;
  681. end;
  682.  
  683. procedure icon_manager.display_icon(i:integer);
  684. begin
  685.   if numicons>0 then
  686.     icons[i]^.display_icon;
  687. end;
  688.  
  689. procedure icon_manager.disposeicon(i:integer);
  690. begin
  691.   if numicons>0 then
  692.   begin
  693.     icons[i]^.disposeicon;
  694.     dispose(icons[i]);
  695.   end;
  696. end;
  697.  
  698. procedure icon_manager.disposeallicons;
  699. var i:integer;
  700. begin
  701.   if numicons>0 then
  702.     for i:=1 to numicons do
  703.       disposeicon(i);
  704. end;
  705.  
  706. procedure icon_manager.showallicons;
  707. var i:integer;
  708. begin
  709.   if numicons>0 then
  710.     for i:=1 to numicons do
  711.       showicon(i);
  712. end;
  713.  
  714. procedure icon_manager.hideallicons;
  715. var i:integer;
  716. begin
  717.   if numicons>0 then
  718.     for i:=1 to numicons do
  719.       hideicon(i);
  720. end;
  721.  
  722. procedure icon_manager.done;
  723. begin
  724. end;
  725.  
  726. begin
  727.   amouse:=driverinstalled;
  728. end.
  729.